package com.example.project.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.project.core.text.Convert;
import com.example.project.entity.IssueRelation;
import com.example.project.entity.Journal;
import com.example.project.entity.User;
import com.example.project.mapper.JournalMapper;
import com.example.project.service.CommonService;
import com.example.project.service.JournalDetailService;
import com.example.project.service.JournalService;
import com.example.project.utils.DateUtils;

/**
 * 问题变更历史主，记录Issue等对象的变更信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-26
 */
@Service
public class JournalServiceImpl extends BaseServiceImpl implements JournalService {

	@Autowired
	private JournalMapper journalMapper;

	@Autowired
	private CommonService commonService;

	@Autowired
	private JournalDetailService journalDetailService;

	/**
	 * 查询问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param id 问题变更历史主，记录Issue等对象的变更信息主键
	 * @return 问题变更历史主，记录Issue等对象的变更信息
	 */
	@Override
	public Journal selectJournalsById(Long id) {
		return journalMapper.selectJournalsById(id);
	}

	/**
	 * 查询问题变更历史主，记录Issue等对象的变更信息列表
	 * 
	 * @param journal 问题变更历史主，记录Issue等对象的变更信息
	 * @return 问题变更历史主，记录Issue等对象的变更信息
	 */
	@Override
	public List<Journal> selectJournalsList(Journal journal) {
		return journalMapper.selectJournalsList(journal);
	}

	/**
	 * 新增问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param journal 问题变更历史主，记录Issue等对象的变更信息
	 * @return 结果
	 */
	@Override
	public int insertJournals(Journal journal) {
		return journalMapper.insertJournals(journal);
	}

	/**
	 * 修改问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param journal 问题变更历史主，记录Issue等对象的变更信息
	 * @return 结果
	 */
	@Override
	public int updateJournals(Journal journal) {
		return journalMapper.updateJournals(journal);
	}

	/**
	 * 批量删除问题变更历史主，记录Issue等对象的变更信息
	 * 
	 * @param ids 需要删除的问题变更历史主，记录Issue等对象的变更信息主键
	 * @return 结果
	 */
	@Override
	public int deleteJournalsByIds(String ids) {
		return journalMapper.deleteJournalsByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除问题变更历史主，记录Issue等对象的变更信息信息
	 * 
	 * @param id 问题变更历史主，记录Issue等对象的变更信息主键
	 * @return 结果
	 */
	@Override
	public int deleteJournalsById(Long id) {
		return journalMapper.deleteJournalsById(id);
	}

	@Override
	public List<Journal> findByJournalizedId(Long journalizedId) {
		// TODO Auto-generated method stub
		List<Journal> journalList = journalMapper.selectJournalsByJournalizedId(journalizedId);
		if (!journalList.isEmpty()) {
			journalList.forEach(info -> {
				Long userid = info.getUpdatedById();
				User user = commonService.getUserInfo(userid);
				if (user != null) {
					info.setUpdatedByName(user.getUserName());
				}
			});
		}
		return journalList;
	}

	@Override
	public Journal getLastJournal(Long issueId) {
		Journal journal = journalMapper.selectLastJournalByJournalizedId(issueId);
		if (journal != null) {
			Long userid = journal.getUpdatedById();
			User user = commonService.getUserInfo(userid);
			if (user != null) {
				journal.setUpdatedByName(user.getUserName());
			}
		}
		return journal;
	}

	/**
	 * 创建关联历史记录
	 */
	@Override
	public void createRelationJournal(long issueId, IssueRelation issueRelation) {
		// 获取当前用户ID
		Long currentUserId = getUserId();

		// 创建Journal记录
		Journal journal = new Journal("Issue", issueId, currentUserId);
		journal.setNotes("");
		journal.setPrivateNotes(0); // 公开笔记
		journal.setCreatedOn(DateUtils.curLocalDateTime());
		journal.setUpdatedOn(DateUtils.curLocalDateTime());
		journal.setUpdatedById(currentUserId);
		// 插入Journal记录
		int insertJournal = journalMapper.insertJournals(journal);
		if (insertJournal > 0 && journal.getId() != null && journal.getId() > 0) {
			// 插入JournalDetail记录
			journalDetailService.createRelationDetails(journal.getId(), issueRelation);
		}
	}

	/**
	 * 创建删除关联关系的历史记录
	 */
	@Override
	public void createDeleteRelationJournal(long issueId, IssueRelation issueRelation) {
		// 获取当前用户ID
		Long currentUserId = getUserId();

		// 创建Journal记录
		Journal journal = new Journal("Issue", issueId, currentUserId);
		journal.setNotes("");
		journal.setPrivateNotes(0); // 公开笔记
		journal.setCreatedOn(DateUtils.curLocalDateTime());
		journal.setUpdatedOn(DateUtils.curLocalDateTime());
		journal.setUpdatedById(currentUserId);
		// 插入Journal记录
		int insertJournal = journalMapper.insertJournals(journal);
		if (insertJournal > 0 && journal.getId() != null && journal.getId() > 0) {
			// 插入删除关联关系的JournalDetail记录
			journalDetailService.createDeleteRelationDetails(journal.getId(), issueRelation);
		}
	}
}
